package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.wms.domain.PickDtl;
import com.xmtlxx.wms.domain.bo.PickDtlBo;
import com.xmtlxx.wms.domain.vo.PickDtlVo;
import com.xmtlxx.wms.mapper.PickDtlMapper;
import com.xmtlxx.wms.service.IPickDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 挑拣任务明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PickDtlServiceImpl implements IPickDtlService {

    private final PickDtlMapper baseMapper;

    /**
     * 查询挑拣任务明细
     *
     * @param id 挑拣任务明细主键
     * @return 挑拣任务明细
     */
    @Override
    public PickDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectPickDtlVoByID(id);
    }

    /**
     * 查询挑拣任务明细列表
     *
     * @param bo 挑拣任务明细
     * @return 挑拣任务明细
     */
    @Override
    public TableDataInfo<PickDtlVo> queryPageList(PickDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<PickDtl> lqw = buildQueryWrapper(bo);
       // Page<PickDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<PickDtlVo> result = baseMapper.selectPagePickDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询挑拣任务明细列表
     *
     * @param bo 挑拣任务明细
     * @return 挑拣任务明细
     */
    @Override
    public List<PickDtlVo> queryList(PickDtlBo bo) {
       // LambdaQueryWrapper<PickDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectPickDtlList(bo);
    }

    private LambdaQueryWrapper<PickDtl> buildQueryWrapper(PickDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PickDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPickId()), PickDtl::getPickId, bo.getPickId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), PickDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), PickDtl::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreId()), PickDtl::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaId()), PickDtl::getAreaId, bo.getAreaId());
        lqw.eq(StringUtils.isNotBlank(bo.getLocatorId()), PickDtl::getLocatorId, bo.getLocatorId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PickDtl::getStatus, bo.getStatus());
        lqw.eq(bo.getCreateTime() != null, PickDtl::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), PickDtl::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, PickDtl::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), PickDtl::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), PickDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增挑拣任务明细
     *
     * @param bo 挑拣任务明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(PickDtlBo bo) {
        PickDtl add = BeanUtil.toBean(bo, PickDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改挑拣任务明细
     *
     * @param bo 挑拣任务明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(PickDtlBo bo) {
        PickDtl update = BeanUtil.toBean(bo, PickDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PickDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除挑拣任务明细
     *
     * @param ids 需要删除的挑拣任务明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
